﻿<!DOCTYPE html>
<html>
<head>
    <title>CGI</title>
</head>
<body>
    <h1>Our Page</h1>
    <script>
        //01
        //array as Stacks (LIFO), 4 last in

        var ratings = [];
        ratings.push(5);
        ratings.push(2, 4);
        console.log(ratings[0]);
        console.log(ratings[1]);
        console.log(ratings[2]);
        console.log(ratings.length);


        //02
        //4 first out
        /*
        var ratings = [];
        ratings.push(5);
        ratings.push(2, 4);
        console.log(ratings.pop());
        console.log(ratings.length);
        */

        //03
        //oposite to pop, shift
        //shift the whole arrayto left and get the first element out of it
        /*
        var ratings = [];
        ratings.push(5);
        ratings.push(2, 4);
        var firstRating = ratings.shift();
        console.log(firstRating);
        console.log(ratings.length);
        */

        //04
        //opposite to push, unshift, put element in begining
        /*
        var ratings = [];
        ratings.push(5);
        ratings.push(2, 4);
        var newCount = ratings.unshift(3);
        console.log(ratings[0]);
        console.log(newCount);
        */

        //05
        //brand new array having elements fron 1 to 6
        /*
        var ratings = [1, 2, 3];
        var newRatings = ratings.concat([4, 5, 6]);
        console.log(newRatings.length);

        var ratings = [1, 2, 3];
        var newRatings = ratings.concat([4, 5, 6], 7, 8);
        console.log(newRatings.length);
        */

        //06 slice
        /*
        //rating subcript 2, ratings[2] = 3 and after that
        var ratings = [1, 2, 3, 4];
        var newRatings = ratings.slice(2);
        console.log(newRatings.toString());

        //after ratings[1]=1 and before ratings[3]=4
        var ratings = [1, 2, 3, 4];
        var newRatings = ratings.slice(1, 3);
        console.log(newRatings.toString());

        //when we call operations on an array, it will create a new array, doesn't modify the existing array
        var ratings = [1, 2, 3, 4];
        var newRatings = ratings.slice(1, 3);
        console.log(ratings.toString());

        //take the last entiers - last two elements
        var ratings = [1, 2, 3, 4];
        var newRatings = ratings.slice(-2);
        console.log(newRatings.toString());
        */

        //07 splice
        //first index is starting point ratings[1] = 2 and second index delete number of items, delete 2 & 3
        //new array is not created, original one is modified
        /*
        var ratings = [1, 2, 3, 4];
        ratings.splice(1, 2);
        console.log(ratings.toString());

        var ratings = [1, 2, 3, 4];
        var removedRating = ratings.splice(0, 1);
        console.log(removedRating.toString());
        console.log(ratings.toString());
        */

        //08
        //splice with 3 args, delete and insert (3rd),2nd element sets to 99, not deleting anything
        /*
        var ratings = [1, 2, 3, 4];
        ratings.splice(2, 0, 99);
        console.log(ratings.toString());

        //higher than 2nd index in splice, all are inserted into array
        var ratings = [1, 2, 3, 4];
        ratings.splice(2, 1, 99, 100, 101);
        console.log(ratings.toString());
        */

        //09
        //reverse all the elements in an aaray
        /*
        var ratings = [1, 2, 3, 4];
        ratings.reverse();
        console.log(ratings.toString());

        var ratings = [4, 1, 3, 2];
        //var ratings = [4, 1, 3, 2, 10]; //bug, sorting by string value of the element
        ratings.sort();
        console.log(ratings.toString());

        var ratings = [4, 1, 3, 2, 10];
        ratings.sort(function (value1, value2) {
            return value1 - value2;
        });
        console.log(ratings.toString());
        */

        //10
        /*
        //3 is at 2nd index of array
        var ratings = [1, 2, 3, 4, 5, 4, 3, 2, 1];
        console.log(ratings.indexOf(3));
        //no conversion takes place, string '3' is in not in array and will give -1
        console.log(ratings.indexOf('3'));
        console.log(ratings.indexOf(66));
        //1 occurs 2 times in array
        var ratings = [1, 2, 3, 4, 5, 4, 3, 2, 1];
        console.log(ratings.lastIndexOf(1));
        */
    </script>
</body>
</html>